package com.springbootblog.controller.fontend;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.web.bind.annotation.*;
import com.springbootblog.service.fontend.ElasticsearchService;

import javax.annotation.Resource;
import java.util.Map;

/**
 * ES部分接口
 * @className AboutController
 * @author camellia
 * @email 805795955@qq.com
 * @date 2024/01/01
 * @version 1.0
 */
@RequestMapping("java")
@RestController
//@CrossOrigin(origins="*",maxAge=3600)
public class ElasticsearchController
{
    // 自动装载（dao接口注入）
    @Resource
    private ElasticsearchService elasticsearchService;

    /**
     * 创建索引
     * @param indexName
     * @return
     * @throws Exception
     */
    @PostMapping("elastic/createIndex")
    public Map<String, Object> createIndex(@RequestParam(defaultValue = "") String indexName) throws Exception
    {
        Map<String, Object> result = elasticsearchService.createIndex(indexName);
        return result;
    }

    /**
     * 获取索引，并判断其是否存在
     * @param indexName
     * @return
     * @throws Exception
     */
    @PostMapping("elastic/indexIsExists")
    public Map<String, Object> indexIsExists(@RequestParam(defaultValue = "") String indexName) throws Exception
    {
        Map<String, Object> result = elasticsearchService.indexIsExists(indexName);
        return result;
    }

    /**
     * 索引删除
     * @param indexName
     * @return
     * @throws Exception
     */
    @PostMapping("elastic/deleteIndex")
    public Map<String, Object> deleteIndex(@RequestParam(defaultValue = "") String indexName) throws Exception
    {
        Map<String, Object> result = elasticsearchService.deleteIndex(indexName);
        return result;
    }//*/

    /**
     * 添加文档（单独添加）
     * @param indexName
     * @return
     * @throws Exception
     */
    /*@PostMapping("elastic/addDocument")
    public Map<String, Object> addDocument(String id, String indexName, String time) throws Exception
    {
        Map<String, Object> result = elasticsearchService.addDocument(id, indexName, time);
        return result;
    }//*/

    /**
     * 文档信息的获取
     * @param indexName
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("elastic/getDocument")
    public Map<String, Object> getDocument(String indexName, String id) throws Exception
    {
        Map<String, Object> result = elasticsearchService.getDocument(indexName, id);
        return result;
    }

    /**
     * 获取文档，判断是否存在
     * @param indexName
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("elastic/documentIsExists")
    public Map<String, Object> documentIsExists(String indexName, String id) throws Exception
    {
        Map<String, Object> result = elasticsearchService.documentIsExists(indexName, id);
        return result;
    }

    /**
     * 更新文档内容
     * @param indexName
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("elastic/updateDocument")
    public Map<String, Object> updateDocument(String indexName, String id) throws Exception
    {
        Map<String, Object> result = elasticsearchService.updateDocument(indexName, id);
        return result;
    }

    /**
     * 删除文档
     * @param indexName
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("elastic/deleteDocument")
    public Map<String, Object> deleteDocument(String indexName, String id, String time) throws Exception
    {
        Map<String, Object> result = elasticsearchService.deleteDocument(indexName, id, time);
        return result;
    }

    /**
     * 条件查询
     * @param indexName
     * @param time
     * @param searchString
     * @return
     * @throws Exception
     */
    @PostMapping("elastic/mapSearch")
    public Map<String, Object> mapSearch(String indexName, String time, String searchString,Integer page) throws Exception
    {
        String[] field = {"content","arttitle","artdesc"};
        Map<String, Object> result = elasticsearchService.mapSearch(indexName, time, searchString,page,field);
        return result;
    }

    /**
     * 批量写入
     * @param time
     * @return
     * @throws Exception
     */
    @PostMapping("elastic/batchAddData")
    public Map<String, Object> batchAddData(String time, String indexName) throws Exception
    {
        Map<String, Object> result = elasticsearchService.batchAddData(time,indexName);
        return result;
    }


}
